﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

namespace Taxes.ExchangeRate
{
    public partial class ExchangeRate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        [DirectMethod]
        public string DeleteRate(int id)
        {
            var bll = new BLL.floor_convert();
            if (bll.Delete(id))
                return "1";
            return "0";
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
          
            int floor;
            decimal rate;
            if(!int.TryParse(this.tfFloor.Text,out floor))
            {
                X.MessageBox.Alert("提示","请输入有效的楼层号").Show();
                tfFloor.Focus();
                return;
            }
            if(!decimal.TryParse(this.tfRate.Text,out rate))
            {
                X.MessageBox.Alert("提示","请输入有效的折算率").Show();
                tfRate.Focus();
                return;
            }
            //判断存不存在该楼层

            var bll = new Taxes.BLL.floor_convert();
            if (bll.ExistFloor(floor, ckLift.Checked ? 1 : 0))
            {
                X.MessageBox.Alert("提示", "该楼层已经存在!").Show();
                return;
            }

            var model = new Taxes.Model.floor_convert()
            {
                floor_num = floor,
                floor_elevator = ckLift.Checked ? 1 : 0,
                floor_converter = rate
            };
            if (tfHidden.Text == "增加")
            {
                if (bll.Add(model) != 0)
                {
                    window1.Hidden = true;
                    tfFloor.Text = string.Empty;
                    tfRate.Text = string.Empty;
                    ckLift.Checked = false;
                    grid.Reload();
                }
            }
            else if(tfHidden.Text=="修改")
            {
                model.ID = Convert.ToInt32(tfIDHidden.Text);
                if (bll.Update(model))
                {
                    window1.Hidden = true;
                    tfFloor.Text = string.Empty;
                    tfRate.Text = string.Empty;
                    ckLift.Checked = false;
                    tfHidden.Text = "增加";
                    grid.Reload();
                }
            }
              
        }
    }
}